﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QImage>
#include <QLabel>
#include <QTextStream>
#include <QPushButton>
#include <QDate>
#include <QFileInfo>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->OpenButton,SIGNAL(clicked()),this,SLOT(open()));
    connect(ui->ConvertButton,SIGNAL(clicked()),this,SLOT(convert()));
    connect(ui->AnimButton,SIGNAL(clicked()),this,SLOT(animation()));
}


void MainWindow::open()
{
    fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"",tr("Files (*.bmp);;All files (*.*)"));
    ui->maintext->setText("File opened! \r\n");
}

void MainWindow::convert()
{
    QString str,str2;
    QFileInfo file2(fileName);
    QFile file(file2.absolutePath()+QDir::separator()+file2.baseName()+".txt");
    file.open(QIODevice::Append);
    QTextStream stream(&file);
    QImage image(fileName);
    int width = image.width();
    int height = image.height();
    QImage* image2 = new QImage(width, height, QImage::Format_ARGB32);
//    stream << width;
//    stream << "x";
//    stream << height;
//    stream << "ST";
//    stream << "\r\n";
    ui->maintext->setText(ui->maintext->text()+"Size: "+str.setNum(width)+"x"+str2.setNum(height)+"\r\n ");

//    for (int i = width-1; i >= 0; i--){
//        for (int j = 0; j < height; j++){
    for (int j = 0; j < height; j++){
        for (int i = 0; i < width; i++){
            str = "";
            QColor pixel = QColor(image.pixel(i,j));
            str.append(QChar(pixel.red()));
            str.append(QChar(pixel.green()));
            str.append(QChar(pixel.blue()));
            if (str.toAscii().toHex() == "0000ff") stream << "1"; // синий
            else if (str.toAscii().toHex() == "00ff00") stream << "2"; // зеленый
            else if (str.toAscii().toHex() == "00ffff") stream << "3"; // бирюзовый
            else if (str.toAscii().toHex() == "ff0000") stream << "4"; // красный
            else if (str.toAscii().toHex() == "ff00ff") stream << "5"; // фиолетовый
            else if (str.toAscii().toHex() == "ffff00") stream << "6"; // желтый
            else if (str.toAscii().toHex() == "ffffff") stream << "7"; // белый
            else if (str.toAscii().toHex() == "000000") stream << "0"; // черный
            else stream << "9";
            image2->setPixel(i,j,pixel.rgb());
        }
        stream << "\r\n";
    }
    QPixmap pixmap(QPixmap::fromImage(*image2));
    ui->label->setPixmap(pixmap.scaled(ui->label->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
//    stream << "EN";
    file.close();
    ui->maintext->setText(ui->maintext->text()+"Success convert! ");
    ui->animtext->setText("WorkFile: "+file2.baseName()+".txt");
}

void MainWindow::animation()
{
    QFileInfo file2(fileName);
    QString file;
    int animindex;
    if (fileName == ""){ //add "if file exist?"
        ui->animtext->setText("File not opened or converted");
        return;
    }
    file = file2.absolutePath()+QDir::separator()+file2.baseName();
    animindex = ui->AnimBox->currentIndex();
    switch (animindex){
        case 0: break;
        case 1: stripes(file);
        case 2: colorstripes(file);
        case 3: changecolor(file);
        default: break;
    }
}

void MainWindow::stripes(QString filepath){
//сдвинуть часть символов
}

void MainWindow::colorstripes(QString filepath){
    QString str;
    int i,j;
    int size;
    QFile file(filepath+".txt");
    file.open(QIODevice::ReadOnly);
    QTextStream stream(&file);
    list.clear();
    size = list.size();
    while (!stream.atEnd())
        list.append(stream.readLine());
    file.close();
    j = 1;
    while (size !=0){
        QFile file2(filepath+str.setNum(j)+".txt");
        file2.open(QIODevice::Append);
        QTextStream stream2(&file2);
        i = 0;
        while (i != 28 || size !=0){
            list.at(size-1).replace("7","3");
            list.at(size-1).replace("0","7");
            list.at(size-1).replace("3","0");
            size--;
        }
        for (int k = 0; k < list.size(); k++)
            stream2 << list.at(k) << endl;
        file2.close();
        j++;
    }
}

void MainWindow::changecolor(QString filepath){
    QString str;
   /* QString colors[7][4] = {("1","2","3","4","5","6","7","0"),
                   ("2","3","4","5","6","7","0","1"),
                   ("3","4","5","6","7","0","1","2"),
                   ("4","5","6","7","0","1","2","3")};*/
    QFile file(filepath+".txt");
    file.open(QIODevice::ReadOnly);
    QTextStream stream(&file);
    list.clear();
    while (!stream.atEnd())
        list.append(stream.readLine());
    file.close();
    for (int i = 0; i < 4; i++){
        QFile file2(filepath+str.setNum(i+1)+".txt");
        file2.open(QIODevice::Append);
        QTextStream stream2(&file2);
        for (int j = 0; j <= list.size(); j++){
            /*list.at(j).replace(str.setNum(colors[i][0]),str.setNum(colors[i][1]));
            list.at(j).replace(str.setNum(colors[i][2]),str.setNum(colors[i][3]));
            list.at(j).replace(str.setNum(colors[i][4]),str.setNum(colors[i][5]));
            list.at(j).replace(str.setNum(colors[i][6]),str.setNum(colors[i][7]));*/
            list.at(j).replace(str.setNum(i),str.setNum(7-i));
            list.at(j).replace(str.setNum(i+1),str.setNum(6-i));
            list.at(j).replace(str.setNum(i+2),str.setNum(5-i));
            list.at(j).replace(str.setNum(i+3),str.setNum(4-i));
            list.at(j).replace(str.setNum(i+4),str.setNum(3-i));
            stream2 << list.at(j) << endl;
        }
        file2.close();
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}
